package com.ttc.mBackup.BackupAgent;

import java.util.Date;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.format.DateFormat;
import android.util.Log;
import android.widget.Toast;

public class DBAdapter {
	/*Bien luu tru ten cac row trong database*/
	public static final String KEY_ROW_ID ="_id";
	public static final String KEY_ROW_NAME = "name";
	public static final String KEY_ROW_VALUE = "value";
	
	/*
	 * Ten file *.db = thoi gian thuc hien backup
	 * Ten table = mySystem
	 * Phien ban cua database = 1 */
	
	public static final  String DATABASE_NAME= "BACKUPSYSTEM";
	public static final String TABLE_NAME = "Systems";
	public static final int  DATABASE_VERSION =1;
	
	/*
	 * Dong String chua lenh thuc thi tao table voi ten System_Db 
	 * cung voi cai row _id, _name, _value
	 * */
	public static final String DATABASE_CREATE = 
		"create table Systems (_id integer primary key autoincrement, " +
		" name text not null, value text not null)";
	
private final Context context;
	
	/*Khoi tao 2 object cua DatabaseHepler va SQLiteDatabase*/
	
	private DatabaseHelper DBHepler;
	private SQLiteDatabase db;

	//Constructor Function
	public DBAdapter(Context ctx)
	{
		this.context=ctx;
		DBHepler= new DatabaseHelper(context);

	}
	
	//Create class DatabaseHelper implements SQLiteOpenHelper for Create table
	
	private static class DatabaseHelper extends SQLiteOpenHelper
	{

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
			Log.v("DBAdapter",DATABASE_NAME );
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			try
			{
				db.execSQL(DATABASE_CREATE);
			}catch(SQLException e){e.printStackTrace();}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS wordstore");
			onCreate(db);
		}
	}
	
	
	///Opens the Database 
	
	public DBAdapter open() throws SQLException
	{
		db = DBHepler.getWritableDatabase();
		return this;
	}
	
	//--- clases the database----
	public void close()
	{
		DBHepler.close();
	}
	
//--- Insert a System Settings into the database---
	
	public long InsertSystem(String name, String value)
	{
		ContentValues initValues= new ContentValues();
		initValues.put(KEY_ROW_NAME, name);
		initValues.put(KEY_ROW_VALUE, value);
		return db.insert(TABLE_NAME, null, initValues);
	}
	
	
//--Deletes aparticular Word ---
	public boolean deleteSysSet(Long rowId)
	{
		return db.delete(TABLE_NAME, KEY_ROW_ID +"="+rowId, null)>0;
	}
/// Retrievies all the Words ---
	
	public Cursor getAllValue()
	{
		return db.query(TABLE_NAME, 
				new String[]{KEY_ROW_ID,KEY_ROW_NAME,KEY_ROW_VALUE},
				null,null,null,null,null);
	}
	
//Retrievies a particular  System set
	public Cursor getSysSet(String Sysname)
	{		Cursor cursor= db.query(TABLE_NAME, 
			new String[]{KEY_ROW_ID,KEY_ROW_NAME,KEY_ROW_VALUE},
			KEY_ROW_NAME+"="+Sysname,null,null,null,null);
		if(cursor!=null)
		{
			cursor.moveToFirst();
		}
		return cursor;
	}
	
//Update System Setting in database
	public boolean updateWords(Long rowId,String name,String value)
	{
		ContentValues content = new ContentValues();
		content.put(KEY_ROW_NAME, name);
		content.put(KEY_ROW_VALUE, value);
		return db.update(TABLE_NAME, content, KEY_ROW_ID+"="+rowId, null)>0;
	}
}
